package research.entity;

import research.domain.Application;
import research.domain.Calander;
import research.domain.Component;
import research.domain.ComponentType;
import research.domain.ComponentValue;
import research.domain.DefaultParameterValue;
import research.domain.FilmType;
import research.domain.Formula;
import research.domain.Mixer;
import research.domain.Parameter;
import research.domain.ParameterValue;
import research.domain.Photo;
import research.domain.Preparation;
import research.domain.Production;
import research.domain.Research;
import research.domain.Result;
import research.domain.Sample;
import research.domain.SampleStabilizer;
import research.domain.Scratch;
import research.domain.Stabilizer;
import research.domain.Strength;
import research.domain.StrengthValue;
import research.domain.Test;
import research.domain.TestParameter;
import research.domain.TestParameterValue;
import research.domain.TestType;
import research.domain.Viscosity;
import research.domain.ViscosityValue;
import research.ui.editors.EditorFactory;

public class EntityFactory {
	
	public static Entity getNew(EntityType type, Entity parent) {
		Entity created = null;
		
		if (type.equals(EntityType.Research)) {
			Research res = Research.getNew();
			res.setApplication(parent instanceof Application ? (Application) parent : null);
			res.setResult(parent instanceof Result ? (Result) parent : null);
			created = res;
		} else if (type.equals(EntityType.Application)) {
			Application res = Application.getNew();
			res.setCalander(parent instanceof Calander ? (Calander) parent : null);
			res.setFilmType(parent instanceof FilmType ? (FilmType) parent : null);
			created = res;
		} else if (type.equals(EntityType.Result))
			created = Result.getNew();
		else if (type.equals(EntityType.Sample)) {
			Sample res = Sample.getNew();
			res.setProduction(parent instanceof Production ? (Production) parent : null);
			res.setFormula(parent instanceof Formula ? (Formula) parent : null);
			created = res;
		} else if (type.equals(EntityType.Photo)) {
			Photo res = Photo.getNew();
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			created = res;
		} else if (type.equals(EntityType.Scratch)) {
			Scratch res = Scratch.getNew();
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			created = res;
		} else if (type.equals(EntityType.Strength)) {
			Strength res = Strength.getNew();
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			created = res;
		} else if (type.equals(EntityType.Viscosity)) {
			Viscosity res = Viscosity.getNew();
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			created = res;
		} else if (type.equals(EntityType.Component)) {
			Component res = Component.getNew();
			res.setComponentType(parent instanceof ComponentType ? (ComponentType) parent : null);
			created = res;
		} else if (type.equals(EntityType.Formula))
			created = Formula.getNew();
		else if (type.equals(EntityType.Calander))
			created = Calander.getNew();
		else if (type.equals(EntityType.FilmType))
			created = FilmType.getNew();
		else if (type.equals(EntityType.TestType))
			created = TestType.getNew();
		else if (type.equals(EntityType.Test)) {
			Test res = Test.getNew();
			res.setTestType(parent instanceof TestType ? (TestType) parent : null);
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			created = res;
		} else if (type.equals(EntityType.Stabilizer)) {
			Stabilizer res = Stabilizer.getNew();
			res.setResearch(parent instanceof Research ? (Research) parent : null);
			created = res;
		} else if (type.equals(EntityType.SampleStabilizer)) {
			SampleStabilizer res = SampleStabilizer.getNew();
			res.setSample(parent instanceof Sample ? (Sample) parent : null);
			res.setStabilizer(parent instanceof Stabilizer ? (Stabilizer) parent : null);
			created = res;
		} else if (type.equals(EntityType.Preparation)) {
			Preparation res = Preparation.getNew();
			res.setMixer(parent instanceof Mixer ? (Mixer) parent : null);
			created = res;
		} else if (type.equals(EntityType.Mixer))
			created = Mixer.getNew();
		else if (type.equals(EntityType.ComponentType))
			created = ComponentType.getNew();
		else if (type.equals(EntityType.Production)) {
			Production res = Production.getNew();
			res.setResearch(parent instanceof Research ? (Research) parent : null);
			res.setCalander(parent instanceof Calander ? (Calander) parent : null);
			res.setPreparation(parent instanceof Preparation ? (Preparation) parent : null);
			created = res;
		}  else if (type.equals(EntityType.ComponentValue))
			created = ComponentValue.getNew();
		else if (type.equals(EntityType.Parameter))
			created = Parameter.getNew();
		else if (type.equals(EntityType.ParameterValue))
			created = ParameterValue.getNew();
		else if (type.equals(EntityType.StrengthValue))
			created = StrengthValue.getNew();
		else if (type.equals(EntityType.TestParameter))
			created = TestParameter.getNew();
		else if (type.equals(EntityType.TestParameterValue))
			created = TestParameterValue.getNew();
		else if (type.equals(EntityType.ViscosityValue))
			created = ViscosityValue.getNew();
		else if (type.equals(EntityType.DefaultParameterValue)) {
			DefaultParameterValue res = DefaultParameterValue.getNew();
			res.setFilmType(parent instanceof FilmType ? (FilmType) parent : null);
			created = res;
		} 
		
		return created;
	}
	
	public static Entity getNew(EntityType type) {
		return getNew(type, null);
	}
	
	public static void openNew(EntityType type, Entity parent) {
		EditorFactory.openEditor(getNew(type, parent));
	}
}
